<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
class CertController extends BaseController {

	public function index() {

		$count = M('certificate')->count();
		$page = getpage($count,10);
		$certificate_arr = M('certificate')->order('certificate_id desc')->limit($page->firstRow, $page->listRows)->select();
		$this->assign('certificate_arr',$certificate_arr);
		$this->assign('page', $page->show());
		$this->display();
	}

	public function cert_details() {
		$data['certificate_id'] = I('get.id');
		$cert_arr = M('certificate')->alias('c')->field('c.*,u.user_name,u.user_phone')->join('left join midi_user as u on u.user_id = c.certificate_user_id')->where($data)->find();
		$this->assign('cert_arr',$cert_arr);
		$this->display();
	}

	public function cert_del() {
		$data['certificate_id'] = I('get.id');
		M('certificate')->where($data)->delete();
		$this->redirect('index');
	}

	public function cert_edit() {
		if (I('post.action') == 'cert_edit_action') {
            $this->cert_edit_action();
            exit();
        }
        $data['certificate_id'] = I('get.id');
		$cert_arr = M('certificate')->where($data)->find();
		$this->assign('cert_arr',$cert_arr);
		$this->display();
	}

	public function cert_edit_action() {
		if ($_POST != array_filter($_POST)) {
			$this->error('选项不能有空值！');
		}
		if (M('certificate')->save($_POST)) {
			$this->success('修改成功','index');
		}else{
			$this->error('没有信息被失败');
		}
	}

	public function cert_add() {
		if (I('post.action') == 'cert_add_action') {
			unset($_POST['action']);
            $this->cert_add_action();
            exit();
        }
		$this->display();
	}

	public function cert_add_action() {
		if ($_POST != array_filter($_POST)) {
			$this->error('选项不能有空值！');
		}
		if (M('certificate')->where('certificate_num = "'.$_POST['certificate_num'].'"')->find()) {
			$this->error('证书编号已存在，请重新添加');
		}
		if ($_POST['certificate_type'] == 1) {
			$data1['certificate_user_id'] = $_POST['certificate_user_id'];
			$data1['certificate_major_id'] = $_POST['certificate_major_id'];
			$data1['certificate_level'] = $_POST['certificate_level'];
			$data1['certificate_type'] = 1;
			if (M('certificate')->where($data1)->find()) {
				$this->error('该专业等级证书已经颁发，不能重复添加');
			}
		}elseif ($_POST['certificate_type'] == 2) {
			$data2['certificate_user_id'] = $_POST['certificate_user_id'];
			$data2['certificate_major_id'] = $_POST['certificate_major_id'];
			$data2['certificate_level'] = $_POST['certificate_level'];
			$data2['certificate_type'] = 2;
			if (M('certificate')->where($data2)->find()) {
				$this->error('迷笛先生资格认证证书已经颁发，不能重复添加');
			}
		}
		if ($cate_id = M('certificate')->add($_POST)) {
			$cate_arr = M('certificate')->where('certificate_id = '.$cate_id)->find();
			if ($_POST['certificate_type'] == 1) {	//如果是专业等级证书
				$condition1['user_id'] = $_POST['certificate_user_id'];
				if ($_POST['certificate_major_id'] == 1) {  //电吉他
					$condition1['user_level_guitar'] = $_POST['certificate_level'];
				}elseif ($_POST['certificate_major_id'] == 2) {	//爵士鼓
					$condition1['user_level_drum'] = $_POST['certificate_level'];
				}elseif ($_POST['certificate_major_id'] == 3) {	//电贝司
					$condition1['user_level_bass'] = $_POST['certificate_level'];
				}
				M('user')->save($condition1);
			}elseif ($_POST['certificate_type'] == 2) {	 //如果是迷笛先生资格证书
				$condition2['mrmidi_user_id'] = $_POST['certificate_user_id'];
				$condition2['mrmidi_major_id'] = $_POST['certificate_major_id'];
				if ($mrmidi = M('mrmidi')->where($condition2)->find()) {
					$mrmidi['mrmidi_certificate_no'] = $cate_arr['certificate_num'];
					$mrmidi['mrmidi_level'] = $cate_arr['certificate_level'];
					$mrmidi['mrmidi_auth_status'] = 2;
					M('mrmidi')->save($mrmidi);
				}else{
					$mrmidi['mrmidi_user_id'] = $_POST['certificate_user_id'];
					$mrmidi['mrmidi_major_id'] = $_POST['certificate_major_id'];
					$mrmidi['mrmidi_certificate_no'] = $cate_arr['certificate_num'];
					$mrmidi['mrmidi_level'] = $cate_arr['certificate_level'];
					$mrmidi['mrmidi_auth_status'] = 2;
					M('mrmidi')->add($mrmidi);
				}
				$data_user['user_id'] = $_POST['certificate_user_id'];
        		$user = M('user')->field('user_id,user_role')->where($data_user)->find();
        		if ($user['user_role'] == 1) {//以前是考生，角色改为2
                    $data_user['user_role'] = 2;
                    M('user')->save($data_user);
                }elseif ($user['user_role'] == 4) {//以前是考官，角色改为3
                    $data_user['user_role'] = 3;
                    M('user')->save($data_user);
                }
			}
			$this->redirect('index');
		}else{
			$this->error('添加证书失败');
		}
	}

	//excel 批量导入
	public function cert_addmany() {
		if (I('post.action') == 'cert_addmany_action') {
			unset($_POST['action']);
            $this->cert_addmany_action();
            exit();
        }
		$this->display();
	}

    public function cert_addmany_action(){
		//xsl上传
		$finfo = upload($_FILES['excel'],6);
        if ($finfo['status'] == 0) {
            if ($finfo['msg'] == 'nofile') {
                $this->error('表格未上传');
            }
            $this->error($finfo['msg']);
        } elseif ($finfo['status'] == 1) {
            $filepath = '.'.$finfo['filepath'];
        }
        $result = import_excel($filepath);
        if ($result == 'wrongfile') {
        	$this->error('文件类型错误');
        }elseif (empty($result)) {
        	$this->error('表格为空');
        }
        foreach ($result as $k => $v) {
        	if (M('certificate')->where('certificate_num = "'.$v[0].'"')->find()) {
				continue;
			}
        	$condition['user_idcard'] = $v[1];
        	if ($user = M('user')->field('user_id')->where($condition)->find()) {
        		$data['certificate_user_id'] = $user['user_id'];
				$data['certificate_major_id'] = $v[11];
				$data['certificate_level'] = $v[12];
				$data['certificate_type'] = 1;
				if (M('certificate')->where($data)->find()) {
					continue;
				}else{
					$data['certificate_num'] = $v[0];
					$data['certificate_time'] = $v[8];
					if (M('certificate')->add($data)) {
						$condition1['user_id'] = $data['certificate_user_id'];
						if ($data['certificate_major_id'] == 1) {  //电吉他
							$condition1['user_level_guitar'] = $data['certificate_level'];
						}elseif ($data['certificate_major_id'] == 2) {	//爵士鼓
							$condition1['user_level_drum'] = $data['certificate_level'];
						}elseif ($data['certificate_major_id'] == 3) {	//电贝司
							$condition1['user_level_bass'] = $data['certificate_level'];
						}
						M('user')->save($condition1);
					}
				}
        	}else{
        		continue;
        	}
        }
        $this->redirect('index');
    }

}